from datetime import datetime
from .database import db
from .base import BaseModel

class UserAddress(BaseModel):
    __tablename__ = 'user_address'

    user_id = db.Column(db.Integer, nullable=False, comment='关联用户ID')
    address_detail = db.Column(db.String(255), nullable=False, comment='地址详情')
    is_default = db.Column(db.Boolean, default=False, comment='是否默认地址（0-否，1-是）')
    id = db.Column(db.Integer, primary_key=True, autoincrement=True, comment='地址ID')
    name = db.Column(db.String(255), nullable=True, comment='姓名')
    phone = db.Column(db.String(255), nullable=True, comment='电话')
    location = db.Column(db.String(255), nullable=True, comment='地址')
    created_at = db.Column(db.DateTime, default=datetime.now, comment='创建时间')
    updated_at = db.Column(db.DateTime, default=datetime.now, onupdate=datetime.now, comment='更新时间')
    is_deleted = db.Column(db.Boolean, default=False, comment='是否删除（0-否，1-是）')

    def __repr__(self):
        return f'<UserAddress {self.id}: {self.address_detail}>'

    def to_dict(self):
        result = super().to_dict()
        # 移除is_deleted字段
        result.pop('is_deleted', None)
        return result